﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace WPFStudy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataTable dt = new DataTable();

            //实例化三个列
            DataColumn dc1 = new DataColumn("factoryName", System.Type.GetType("System.String"));

            DataColumn dc2 = new DataColumn("factoryPosition", System.Type.GetType("System.String"));

            DataColumn dc3 = new DataColumn("factoryHistory", Type.GetType("System.String"));
            dt.Columns.Add(dc1);

            dt.Columns.Add(dc2);

            dt.Columns.Add(dc3);

            //添加三条数据　　　　

            DataRow row = dt.NewRow();
            row["factoryName"] = "微软";
            row["factoryPosition"] = "北京";
            row["factoryHistory"] = "100years";
            dt.Rows.Add(row);

            row = dt.NewRow();

            row["factoryName"] = "苹果";
            row["factoryPosition"] = "上海";
            row["factoryHistory"] = "200years";
            dt.Rows.Add(row);

            row = dt.NewRow();

            row["factoryName"] = "谷歌";
            row["factoryPosition"] = "深圳";
            row["factoryHistory"] = "300years";
            dt.Rows.Add(row);

            //grid1.Columns.Add(new DataGridTextColumn() { Header = "公司名称", Binding = new Binding("factoryName"), Width = new DataGridLength(2, DataGridLengthUnitType.Star) });      //为DataGrid生成百分比列宽的列，相当于xaml中设置Width = "2*"
            //grid1.Columns.Add(new DataGridTextColumn() { Header = "所在城市", Binding = new Binding("factoryPosition"), Width = new DataGridLength(2, DataGridLengthUnitType.Star) });      //为DataGrid生成百分比列宽的列，相当于xaml中设置Width = "2*"
            //grid1.Columns.Add(new DataGridTextColumn() { Header = "公司历史", Binding = new Binding("factoryHistory"), Width = new DataGridLength(2, DataGridLengthUnitType.Star) });      //为DataGrid生成百分比列宽的列，相当于xaml中设置Width = "2*"
            //grid1.ItemsSource = dt.AsDataView();

            List<Company> list = new List<Company>();
            list.Add(new Company("百度","北京","30"));
            list.Add(new Company("阿里巴巴", "杭州", "20"));
            list.Add(new Company("腾讯", "深圳", "25"));

            grid1.Columns.Add(new DataGridTextColumn() { Header = "公司名称", Binding = new Binding("name"), Width = new DataGridLength(2, DataGridLengthUnitType.Star) });
            grid1.Columns.Add(new DataGridTextColumn() { Header = "所在城市", Binding = new Binding("city"), Width = new DataGridLength(2, DataGridLengthUnitType.Star) });
            grid1.Columns.Add(new DataGridTextColumn() { Header = "公司历史", Binding = new Binding("age"), Width = new DataGridLength(2, DataGridLengthUnitType.Star) }); 

            grid1.DataContext = list;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem s = (MenuItem)sender;
            s.IsChecked = !s.IsChecked;
        }

        private void CloseSys(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    public class Company
    {
        public string name { get; set; }
        public string city { get; set; }
        public string age { get; set; }

        public Company(string name,string city,string age)
        {
            this.name = name;
            this.city = city;
            this.age = age;
        }
    }
}
